/* 
 * File:   main.c
 * Author: Jung
 *
 * Created on 30 de Março de 2012, 15:42
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>



#include <time.h>

#define NUM_THREADS 5

char mensagem[20];
int cont = 0;

typedef struct  {
    char id[8];
    int etapa;
    int tempo;
} requisicao;

int contaThead = -1;
char *ips[5];

//192.168.100.93
//192.168.100.94
//192.168.100.95
//192.168.100.96
//192.168.100.97

void teste() {
    
    ips[0] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[0], "127.0.0.1");
     ips[1] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[1], "127.0.0.2");
     ips[2] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[2], "127.0.0.3");
     ips[3] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[3], "127.0.0.4");
     ips[4] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[4], "127.0.0.5");
    
}




void *PrintHello(void *threadid) { 
    contaThead++;
    
    int tempo = 3;
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;
    requisicao r;
    
    char ch[20];
    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    server_address.sin_family = AF_INET;
    //printf("%c\n",ips)
    server_address.sin_addr.s_addr = inet_addr(ips[contaThead]);
    server_address.sin_port = 9734;
    
    server_len= sizeof(server_address);
    bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
    listen(server_sockfd, 5);
    while(1) {
        printf("Servidor  %d  esperando \n", threadid);
        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_address, &client_len);
        
        tempo = (int)(random()%9);
        
        read(client_sockfd, &ch, sizeof(char)*30);
        
        //strcpy(ch,"vvvvvvvvvvvvv");
        //printf("fff %s\n", *ch);
        
        
        //sleep(1);
        
        //r.tempo = 9;
        ch[9] = ch[9]+1;
        ch[11] = ch[11] + tempo;
        write(client_sockfd, &ch, sizeof(char)*30);
        
        printf("Servidor  %d  respondeu com %s  para  %i    >>> %d \n", threadid,  ch, client_sockfd, cont++ );
        
        close(client_sockfd);
    }
    
    
}

int main(int argc, char*argv[]) {
    
    teste();
    
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    for(t=0; t<NUM_THREADS; t++){
        //printf("In main: creating thread %ld\n", t);
        
        //printf("%d\n",rand());
        
        rc= pthread_create(&threads[t], NULL, PrintHello, (void*)t);
        if(rc){
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }
    pthread_exit(NULL);

    return (EXIT_SUCCESS);
}

